import { Action } from "./action/Action";
import { Wait } from "./action/Wait";
import { BehaviorTree } from "./BehaviorTree";
import { Blackboard } from "./Blackboard";
import { BTNode } from "./BTNode";
import { BTState } from "./BTState";
import { Selector } from "./composite/Selector";
import { Sequence } from "./composite/Sequence";
import { Condition } from "./condition/Condition";

// 示例行为树创建函数
export function createSampleBehaviorTree(): BehaviorTree {
    // 创建具体节点实例（不再使用抽象类）
    const checkHasTarget = new Condition("HasTarget", bb => bb.get("target") !== undefined);
    const moveToTarget = new MoveToAction("MoveToTarget", "target");
    const attackTarget = new AttackAction("AttackTarget", "target");
    const patrol = new PatrolAction("Patrol", ["Point A", "Point B"]);
    const wait = new Wait("Wait", 2000);
    
    // 组合节点
    const attackSequence = new Sequence("AttackSequence", [
        checkHasTarget,
        moveToTarget,
        attackTarget
    ]);
    
    const patrolSequence = new Sequence("PatrolSequence", [
        patrol,
        wait
    ]);
    
    const rootSelector = new Selector("RootSelector", [
        attackSequence,
        patrolSequence
    ]);
    
    // 创建行为树
    const blackboard = new Blackboard();
    return new BehaviorTree(rootSelector, blackboard);
}
// 具体动作节点实现
export class MoveToAction extends BTNode {
    private targetKey: string;
    private speed: number;
    
    constructor(name: string, targetKey: string, speed: number = 1) {
        super(name);
        this.targetKey = targetKey;
        this.speed = speed;
    }
    
    tick(blackboard: Blackboard): BTState {
        const target = blackboard.get<any>(this.targetKey);
        
        if (!target) {
            this.state = BTState.FAILURE;
            return this.state;
        }
        
        console.log(`Moving to target: ${target} at speed ${this.speed}`);
        this.state = BTState.SUCCESS;
        return this.state;
    }
    
    interrupt(): void {
        if (this.state === BTState.RUNNING) {
            this.state = BTState.INTERRUPTED;
            console.log("Movement interrupted!");
        }
    }
    
    reset(): void {
        this.state = BTState.FAILURE;
    }
}

export class AttackAction extends BTNode {
    private targetKey: string;
    
    constructor(name: string, targetKey: string) {
        super(name);
        this.targetKey = targetKey;
    }
    
    tick(blackboard: Blackboard): BTState {
        const target = blackboard.get<any>(this.targetKey);
        
        if (!target) {
            this.state = BTState.FAILURE;
            return this.state;
        }
        
        console.log(`Attacking target: ${target}`);
        this.state = BTState.SUCCESS;
        return this.state;
    }
    
    interrupt(): void {
        if (this.state === BTState.RUNNING) {
            this.state = BTState.INTERRUPTED;
            console.log("Attack interrupted!");
        }
    }
    
    reset(): void {
        this.state = BTState.FAILURE;
    }
}

export class PatrolAction extends BTNode {
    private waypoints: string[];
    private currentWaypoint: number = 0;
    
    constructor(name: string, waypoints: string[]) {
        super(name);
        this.waypoints = waypoints;
    }
    
    tick(blackboard: Blackboard): BTState {
        if (this.waypoints.length === 0) {
            this.state = BTState.SUCCESS;
            return this.state;
        }
        
        const waypoint = this.waypoints[this.currentWaypoint];
        console.log(`Patrolling to: ${waypoint}`);
        
        this.currentWaypoint = (this.currentWaypoint + 1) % this.waypoints.length;
        this.state = BTState.SUCCESS;
        return this.state;
    }
    
    interrupt(): void {
        if (this.state === BTState.RUNNING) {
            this.state = BTState.INTERRUPTED;
            console.log("Patrol interrupted!");
        }
    }
    
    reset(): void {
        this.state = BTState.FAILURE;
        this.currentWaypoint = 0;
    }
}

// 复杂行为树示例
export function createComplexBehaviorTree(): BehaviorTree {
    // 创建条件
    const hasTarget = new Condition("HasTarget", bb => bb.get("target") !== undefined);
    const isLowHealth = new Condition("IsLowHealth", bb => (bb.get<number>("health") || 100) < 30);
    
    // 创建动作
    const flee = new MoveToAction("Flee", "safeLocation", 2.5);
    const heal = new Wait("Heal", 5000);
    const moveToTarget = new MoveToAction("MoveTo", "target");
    const attack = new AttackAction("Attack", "target");
    const patrol = new PatrolAction("Patrol", ["A", "B", "C"]);
    const wait = new Wait("Wait", 3000);
    
    // 组合节点
    const fleeSelector = new Selector("FleeOrHeal", [
        flee,
        heal
    ]);
    
    const lowHealthSequence = new Sequence("LowHealthActions", [
        isLowHealth,
        fleeSelector
    ]);
    
    const combatSequence = new Sequence("CombatActions", [
        hasTarget,
        moveToTarget,
        attack
    ]);
    
    const patrolSequence = new Sequence("PatrolActions", [
        patrol,
        wait
    ]);
    
    // 根节点
    const rootSelector = new Selector("RootSelector", [
        lowHealthSequence,
        combatSequence,
        patrolSequence
    ]);
    
    // 创建行为树
    const blackboard = new Blackboard();
    blackboard.set("health", 100);
    blackboard.set("safeLocation", "Base Camp");
    return new BehaviorTree(rootSelector, blackboard);
}

// 测试示例
function testBehaviorTree() {
    // 创建行为树
    const tree = createSampleBehaviorTree();
    const bb = tree.getBlackboard();
    
    console.log("=== 初始状态 ===");
    tree.update(); // 应该执行巡逻
    
    console.log("\n=== 设置目标 ===");
    bb.set("target", "Enemy1");
    tree.update(); // 应该攻击目标
    
    console.log("\n=== 移除目标 ===");
    bb.delete("target");
    tree.update(); // 应该返回巡逻
    
    console.log("\n=== 测试中断 ===");
    bb.set("target", "Enemy2");
    tree.update(); // 开始攻击
    tree.interrupt(); // 中断当前行为
    console.log(`中断后状态: ${tree.getRootState()}`);
}

// 运行测试
// testBehaviorTree();